<?php
/**
 * 
 * 子类分类目录
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-3-5
 * model() 创建模型对象，静态
 * tableName() 返回表名
*/

class Category extends CActiveRecord{
	
	public $page_list = array();//分页数组
	public $infos_count = 0;//统计查询个数
	/**
	 *返回当前模型对象的方法 
	 */
	public static function model($className = __CLASS__){
		return parent::model($className);
	}
	
	/**
	 * 返回当前表名
	 */
	public function tableName(){
		return '{{Category}}';
	}
	
	
	/**
	 * 搜索+分页
	 */
	public function categorySearch($category){
		$_sql = '';
		$cmodel = new CCCModel();
		$_sql .= "SELECT * FROM {{category}} WHERE 1=1";
		$nextpage = "";
		if(!empty($category['keyword'])){
			$nextpage .= "/keyword=".$category['keyword'];
			$_sql .= " AND category_name LIKE '%".$category['keyword']."%' OR category_info LIKE '%".$category['keyword']."%'";
			
		} 
		$_sql_count = str_replace('SELECT *','SELECT count(*)',$_sql);
			$per = 10;
			$this->infos_count = $cmodel->sel($_sql_count);
			$cnt = $this->infos_count[0]['count(*)'];
			$page = new Pagination($cnt,$per);
			$_sql .= " $page->limit";
			$info = $cmodel->sel($_sql);
			$page_list[0] = $page->fpage(array(3));
			$page_list[1] = $page->fpage(array(4));
			$page_list[2] = $page->fpage(array(5));
			$page_list[3] = $page->fpage(array(6));
			$page_list[4] = $page->fpage(array(7));
			
			$this->page_list[0] = $page_list[0];
			$this->page_list[1] = $page_list[1];
			$this->page_list[2] = $page_list[2];
			$this->page_list[3] = $page_list[3];
			$this->page_list[4] = $page_list[4];
			
			return $info;
	}
	
/**
	 * 遍历参数属性
	 */

	public function ParamList($category_id){
		$cmodel = new CCCModel();	
		$_sql = "SELECT a.param_id,a.param_name,(SELECT b.cat_param_id FROM xy_cat_param b WHERE a.param_id = b.param_id and b.active = 1 and b.category_id = $category_id) as cat_param_id FROM xy_param a WHERE a.active = 1 AND a.parent_id = 0";
		$row = $cmodel->sel($_sql);
		return $row;
	}
	
	
	/**
	 * 遍历配置属性
	 */

	public function AttrList($category_id){
		$cmodel = new CCCModel();	
		$_sql = "SELECT a.attr_id,a.attr_name,(SELECT b.cat_attr_id FROM xy_cat_attr b WHERE a.attr_id = b.attr_id and b.active = 1 and b.category_id = $category_id) as cat_attr_id FROM xy_attr a WHERE a.active = 1";
		$row = $cmodel->sel($_sql);
		return $row;
	}
	
	
/**
	 * 为分类配置参数
	 */
	public function updParam($category_id,$arrParam){
		$cmodel = new CCCModel();	
		$_sql = "UPDATE {{cat_param}} SET active = 0 WHERE category_id=".$category_id;
		$cmodel->upd($_sql);
		foreach ($arrParam as $_v){ 		
			$_sql = "SELECT * FROM {{cat_param}} WHERE category_id=".$category_id." AND param_id=".$_v;
			$result = $cmodel -> sel($_sql);
			if(!empty($result)){
				$_sql = "UPDATE {{cat_param}} SET active = 1 WHERE category_id=".$category_id." AND param_id=".$_v;
				$result = $cmodel->upd($_sql);
			}else{
				$_sql = "INSERT INTO  {{cat_param}}(`category_id`,`param_id`) VALUES('".$category_id."','".$_v."')";
				$result = $cmodel->upd($_sql);
			}
		} 			
		return $result;
	}
	
	/**
	 * 为分类配置属性
	 */
	public function updAttr($category_id,$arrAttr){
		$cmodel = new CCCModel();	
		$_sql = "UPDATE {{cat_attr}} SET active = 0 WHERE category_id=".$category_id;
		$cmodel->upd($_sql);
		foreach ($arrAttr as $_v){ 		
			$_sql = "SELECT * FROM {{cat_attr}} WHERE category_id=".$category_id." AND attr_id=".$_v;
			$result = $cmodel -> sel($_sql);
			if(!empty($result)){
				$_sql = "UPDATE {{cat_attr}} SET active = 1 WHERE category_id=".$category_id." AND attr_id=".$_v;
				$result = $cmodel->upd($_sql);
			}else{
				$_sql = "INSERT INTO  {{cat_attr}}(`category_id`,`attr_id`) VALUES('".$category_id."','".$_v."')";
				$result = $cmodel->upd($_sql);
			}
		} 			
		return $result;
	}
	
	
	/**
	 * 查找所有category不带条件
	 */
	public function all_category(){
		$cmodel = new CCCModel();
		$_sql = "SELECT * FROM {{category}} ORDER BY sort ASC";
		$_sql_count = str_replace('SELECT *','SELECT count(*)',$_sql);
		$per = 10;
		$this->infos_count = $cmodel->sel($_sql_count);
		$cnt = $this->infos_count[0]['count(*)'];
		$page = new Pagination($cnt,$per);
		$_sql .= " $page->limit";
		$info = $cmodel->sel($_sql);
		$page_list[0] = $page->fpage(array(3));
		$page_list[1] = $page->fpage(array(4));
		$page_list[2] = $page->fpage(array(5));
		$page_list[3] = $page->fpage(array(6));
		$page_list[4] = $page->fpage(array(7));
		
		$this->page_list[0] = $page_list[0];
		$this->page_list[1] = $page_list[1];
		$this->page_list[2] = $page_list[2];
		$this->page_list[3] = $page_list[3];
		$this->page_list[4] = $page_list[4];
		
		return $info;
		
		
		
		
		
		return $result;
	}
	
/**
	 * 按照sort排序(参数)
	 */
	public function paramsort($_data){
		$cmodel = new CCCModel();
		foreach($_data as $_k => $_v){
			$_sql = "UPDATE {{param}} SET param_sort=".$_v." WHERE param_id=".$_k;
			$row = $cmodel->upd($_sql);
		}
	}
	
	/**
	 * 按照sort排序(属性)
	 */
	public function attrsort($_data){
		$cmodel = new CCCModel();
		foreach($_data as $_k => $_v){
			$_sql = "UPDATE {{attr}} SET attr_sort=".$_v." WHERE attr_id=".$_k;
			$row = $cmodel->upd($_sql);
		}
	}
	
	
	/**
	 * 按照sort排序(分类)
	 */
	public function sort($_data){
		$cmodel = new CCCModel();
		foreach($_data as $_k => $_v){
			$_sql = "UPDATE {{category}} SET sort=".$_v." WHERE category_id=".$_k;
			$a = $cmodel->upd($_sql);
		}
	}
	
	/**
	 * 查询所有Catetory
	 */
	public function all(){
		$cmodel = new CCCModel();
		$result = $cmodel->sel("SELECT * FROM {{category}} ORDER BY sort ASC");
		return $result;
	}
	public function add(){
		$goodsTypes = Yii::app()->getDb()->createCommand()  
        ->select('*')  
        ->from('xy_category')  
        ->where('category_name=:status', array(  
            ':status' =>$_POST['category_name']  
        ))->queryAll(); 
        if(!empty($goodsTypes)){
        	echo "<script>alert('分类信息已经存在');</script>";
        	echo "分类信息已经存在,请返回";
        	exit;
        }
		$row = Yii::app()->getDb()->createCommand()->insert('xy_category',array(
				'category_name' => $_POST['category_name'],
				'category_info' => 	$_POST['category_info'],
				'sort' => $_POST['sort'],
		));
		return $row;
	}
	
	
}
	
?>